﻿using VirtualTournaments.Business.Players;
using VirtualTournaments.Business.Tournaments.Exceptions;
using System;
using System.Linq;
using System.Collections.Generic;

namespace VirtualTournaments.Business.Tournaments
{
	public class Team
	{
		public enum Role { GOALKEEPER, DEFENDER_1, DEFENDER_2, MIDFIELDER_1, MIDFIELDER_2, FORWARD }
		internal const int PLAYERS_PER_TEAM = 6;

		private string _name;
		private List<Player> _players;

		public Team(string name)
		{
			this.Name = name;
		}

		public Team(string name, IEnumerable<Player> players)
		{
			this.Name = name;
			Players = players;
		}

		public string Name
		{
			get { return _name; }
			set { _name = value; }
		}

		public virtual IEnumerable<Player> Players
		{
			get { return _players; }
			protected set
			{
				if (value.Count() != PLAYERS_PER_TEAM)
					throw new InvalidPlayersNumberException();
				_players = value.ToList();
			}
		}

		public Player getPlayer(Role role)
		{
			return _players[(int)role];
		}

		public void SetPlayer(Role role, Player player)
		{
			if (_players == null)
				_players = new List<Player>(new Player[PLAYERS_PER_TEAM]);
			_players[(int)role] = player;
		}

		public override bool Equals(object obj)
		{
			Team another = obj as Team;
			if (obj == null)
				return false;
			if (another == this)
				return true;
			if (another.Name == this.Name)
				return true;
			return false;
		}

		public override int GetHashCode()
		{
			return base.GetHashCode();
		}
	}
}
